﻿using System;

namespace SXICE.Core.Infrastructure
{
    /// <summary>
    /// 缓存基接口
    /// </summary>
    public interface ICacheBase
    {
        /// <summary>
        /// 设置
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        /// <param name="slidingExpiration">平滑过期时间</param>
        /// <param name="absoluteExpiration">绝对过期时间</param>
        void Set<T>(string key, T value, TimeSpan? slidingExpiration = null, DateTime? absoluteExpiration = null);

        /// <summary>
        /// 获取
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">键</param>
        /// <param name="obj">获取的结果</param>
        /// <returns>是否成功获取</returns>
        bool TryGet<T>(string key, out T obj);

        /// <summary>
        /// 获取或者添加
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">键</param>
        /// <param name="func">要添加的对象回调函数</param>
        /// <param name="slidingExpiration">平滑过期时间</param>
        /// <param name="absoluteExpiration">绝对过期时间</param>
        /// <returns>结果</returns>
        T GetOrAdd<T>(string key, Func<T> func, TimeSpan? slidingExpiration = null, DateTime? absoluteExpiration = null);

        /// <summary>
        /// 添加或更新
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">键</param>
        /// <param name="obj">对象</param>
        void AddOrUpdate<T>(string key, T obj);

        /// <summary>
        /// 是否已存在
        /// </summary>
        /// <param name="key">键</param>
        /// <returns>结果</returns>
        bool Exists(string key);

        /// <summary>
        /// 移除
        /// </summary>
        /// <param name="key">要移除的键</param>
        void Remove(string key);
    }
}